var index_ligne = 0;
var prelev_type = '';

function init()
{
	document.saisie_prelev.liste_mois.options[0].selected = true;
	document.saisie_prelev.cotiz_std.value = "";
	document.saisie_prelev.prelev_type.checked = false;
}

function ajoutLigneSaisie()
{
	index_ligne = index_ligne + 1;

	var elem = document.getElementById("cotizSpecific");
	var newDiv = document.createElement("span");
	var ligneHtml = "<label>Cotisation sp&eacute;cifique: </label><input id=\"spec_!CODE_cotiz\" class=\"short\" type=\"text\"/> (&euro;), Association Dept.: <input id=\"spec_!CODE_ad\" class=\"short\" type=\"text\"/> <img onclick=\"ajoutLigneSaisie()\" src=\"images/add.gif\" alt=\"+\"/><br/>";

	if (index_ligne < 10)
		ligneHtml = ligneHtml.replace(/!CODE/g, "0" + index_ligne);
	if (9 < index_ligne < 100)
		ligneHtml = ligneHtml.replace(/!CODE/g, "" + index_ligne);

	if (index_ligne == 1){
		var elem2 = document.getElementById("marqueur_specific");
		elem.removeChild(elem2);
		newDiv.innerHTML = ligneHtml; 
		elem.appendChild(newDiv);
	} else {
		newDiv.innerHTML = ligneHtml; elem.appendChild(newDiv);
	}
}

function validationFormulaire()
{
	var formulaire_valide = true;
	var prelev_standard = new Array (2);
	var tableau_prelev_spec = new Array();

	prelev_standard[0] = document.saisie_prelev.liste_mois.value;
	prelev_standard[1] = document.saisie_prelev.cotiz_std.value;

	if (chaineEstNumeric(prelev_standard[1])) {
		prelev_standard[1] = formatageNumeric(prelev_standard[1]);
		tableau_prelev_spec[0] = prelev_standard;
		document.saisie_prelev.cotiz_std.className = "normal";
	} else {
		document.saisie_prelev.cotiz_std.className = "error";
		formulaire_valide = false;
	}

	var elem_cotiz;
	var elem_assoc;
	var obj_cotiz;
	var obj_assoc;
	
	var tag_prefix = "spec_";
	var tag_cotiz_suffix = "_cotiz"
	var tag_asso_suffix = "_ad"

	for (i = 1; i < index_ligne + 1; i++) {
		var ligne_prelev_spec = new Array(2);
		if (i < 10) {
			elem_cotiz = tag_prefix + "0" + i + tag_cotiz_suffix;
			elem_assoc = tag_prefix + "0"+ i + tag_asso_suffix;
		} else if (9 < i && i < 100) {
			elem_cotiz = tag_prefix + i + tag_cotiz_suffix;
			elem_assoc = tag_prefix + i + tag_asso_suffix;
		}

		obj_cotiz = document.getElementById(elem_cotiz);
		obj_assoc = document.getElementById(elem_assoc);

		ligne_prelev_spec[0]=obj_cotiz.value;
		ligne_prelev_spec[1]=obj_assoc.value;

		if (chaineEstNumeric(ligne_prelev_spec[0])) {
			obj_cotiz.className = "normal"; 
			ligne_prelev_spec[0] = formatageNumeric(ligne_prelev_spec[0]);
		} else {
			obj_cotiz.className = "error";
			formulaire_valide = false;
		}
		if (chaineEstAsso(ligne_prelev_spec[1])) {
			obj_assoc.className = "normal";
		} else {
			obj_assoc.className = "error";
			formulaire_valide = false;
		}

		tableau_prelev_spec[i]=ligne_prelev_spec;
	}
   
	if (formulaire_valide) {
		var elem = document.getElementById("debug_output");
		var elem2 = document.getElementById("debug_output").firstChild;
		if (elem2 != null)
			elem.removeChild(elem2);
		var newElem = document.createElement("span");
		newElem.innerHTML = JSON.stringify(tableau_prelev_spec);
		elem.appendChild(newElem);

		send_request(JSON.stringify(tableau_prelev_spec));
	}
}

function chaineEstNumeric(chaine)
{
	re = /^([1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[0-9],[1-9]|[1-9][0-9],[0-9]|[1-9][0-9][0-9],[0-9]|[0-9],[0-9][0-9]|[1-9][0-9],[0-9][0-9]|[1-9][0-9][0-9],[0-9][0-9])$/
	return re.test(chaine);
}
function chaineEstAsso(chaine)
{
	re = /^([0-9][0-9]|[0-9])$/
	return re.test(chaine);
}

function formatageNumeric(numeric)
{
	var chaine = "";

	var re = /^[1-9]$/;
	if (re.test(numeric))
		chaine = "0000" + numeric + "00";

	re = /^[1-9][0-9]$/
	if (re.test(numeric))
		chaine = "000" + numeric + "00";

	re = /^[1-9][0-9][0-9]$/
	if (re.test(numeric))
		chaine = "00" + numeric + "00";

	re = /^[0-9],[1-9]$/
	if (re.test(numeric))
		chaine = "0000" + numeric + "0";

	re = /^[1-9][0-9],[0-9]$/
	if (re.test(numeric))
		chaine = "000" + numeric + "0";

	re = /^[1-9][0-9][0-9],[0-9]$/
	if (re.test(numeric))
		chaine = "00" + numeric + "0";

	re = /^[0-9],[0-9][0-9]$/
	if (re.test(numeric))
		chaine = "0000" + numeric;

	re = /^[1-9][0-9],[0-9][0-9]$/
	if (re.test(numeric))
		chaine = "000" + numeric;

	re = /^[1-9][0-9][0-9],[0-9][0-9]$/
	if (re.test(numeric))
		chaine = "00" + numeric;

	return chaine.replace(",","");

}

function config_prelev_type(type)
{
	prelev_type = type;
}

function send_request (json_chaine)
{
	send=false;

	if (prelev_type=="actif") {
		target_page = "Prelevement-Actif.aspx"
		send = true;
	} else if (prelev_type=="retraite") {
		target_page = "Prelevement-Retraite.aspx"
		send = true;
	} else {
		alert("Le type de prelevement doit etre selectionne " /*+ document.saisie_prelev.prelev_type.value*/)
		send = false;
	}

	if (send)
		location.href= target_page + "?input=" + json_chaine
}
